package io.renren.modules.clienthome.service.impl;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.CommonBusinessStatus;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.clienthome.entity.TalentsHomeVo;
import io.renren.modules.clienthome.entity.WorkJobHomeVO;
import io.renren.modules.clienthome.service.ClientHomeService;
import io.renren.modules.common.dao.*;
import io.renren.modules.common.entity.*;
import io.renren.modules.companys.dao.CompanyDao;
import io.renren.modules.companys.entity.CompanyEntity;
import io.renren.modules.telnets.dao.TalentsRepertoryDao;
import io.renren.modules.telnets.entity.TalentsRepertoryEntity;
import io.renren.modules.workjobs.dao.WorkJobDao;
import io.renren.modules.workjobs.entity.WorkJobEntity;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;

@Service
public class ClientHomeServiceImpl implements ClientHomeService {

    @Autowired
    private WorkJobDao workJobDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private ProvincesDao provincesDao;
    @Autowired
    private CitiesDao citiesDao;
    @Autowired
    private AreasDao areasDao;

    @Autowired
    private TradeCategoryDao tradeCategoryDao;
    @Autowired
    private TradeClassifyDao tradeClassifyDao;
    @Autowired
    private TradeJobDao tradeJobDao;

    @Autowired
    private EducationDao educationDao;
    @Autowired
    private TalentsRepertoryDao talentsRepertoryDao;



    @Override
    public R excellentWorkJob() {
        List<WorkJobHomeVO> resultData = new ArrayList<>();

        QueryWrapper<WorkJobEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("job_is_pass",1)
                .eq("job_approve_status", CommonBusinessStatus.POSITION_HAVE_APPROVE)
                .eq("job_recruit_status",CommonBusinessStatus.POSITION_RECRUIT_ING);

        /*查询出来复合要求的职位信息*/
        List<WorkJobEntity> workJobEntitieList = workJobDao.selectList(wrapper);


        /*加载所有的企业信息*/
        List<CompanyEntity> companyEntitieList = companyDao.selectList(null);



        for (WorkJobEntity workJobItem : workJobEntitieList){

            WorkJobHomeVO workJobHomeVO = new WorkJobHomeVO();


            for (int i=0;i<companyEntitieList.size();i++){
                if (companyEntitieList.get(i).getCreateUserId().equals(workJobItem.getCreateUserId())) {
                    workJobHomeVO.setCompanyName(companyEntitieList.get(i).getCompanyName());
                }
            }


            workJobHomeVO.setWorkJobName(workJobItem.getJobName());
            workJobHomeVO.setSalaryStart(workJobItem.getJobSalaryStart());
            workJobHomeVO.setSalaryEnd(workJobItem.getJobSalaryEnd());

            QueryWrapper<ProvincesEntity> provicesWrapper = new QueryWrapper<>();
            QueryWrapper<CitiesEntity> citiesWrapper = new QueryWrapper<>();
            QueryWrapper<AreasEntity> areasWrapper = new QueryWrapper<>();

            provicesWrapper.eq("provinceid",workJobItem.getProvincesId());
            citiesWrapper.eq("cityid",workJobItem.getCitysId());
            areasWrapper.eq("areaid",workJobItem.getAreasId());

            ProvincesEntity provincesEntity = provincesDao.selectOne(provicesWrapper);
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesWrapper);
            AreasEntity areasEntity = areasDao.selectOne(areasWrapper);

            workJobHomeVO.setProvinces(provincesEntity.getProvincename());
            workJobHomeVO.setCityes(citiesEntity.getCityname());
            workJobHomeVO.setAreas(areasEntity.getAreaname());

            Date createTime = workJobItem.getCreateTime();
            long time = createTime.getTime();
            long millis = System.currentTimeMillis();

            long diffTime  = millis - time;

            //直接取整
            long day = diffTime / (24 * 60 * 60 * 1000);
            workJobHomeVO.setDayLong(String.valueOf(day));

            resultData.add(workJobHomeVO);
        }
        return R.ok().put("data",resultData);
    }

    @Override
    public R talentsInfo() {
        /*用来存储人才信息的容器*/
        List<TalentsHomeVo> talentsHomeVoDataList = new ArrayList<>();

        /*所有的人才信息*/
        List<TalentsRepertoryEntity> talentsRepertoryEntities = talentsRepertoryDao.selectList(null);

        //对数组进行自然排序
        Collections.sort(talentsRepertoryEntities);

        for (TalentsRepertoryEntity itemTalent: talentsRepertoryEntities){

            TalentsHomeVo talentsHomeVo = new TalentsHomeVo();

            String advator = itemTalent.getAdvator();
            if (advator!=null){
                InputStream in = null;
                byte[] data = null;
                try {
                    in = new FileInputStream(new File(advator));
                    data = new byte[in.available()];
                    in.read(data);
                    in.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String base64 = "data:image/jpeg;base64," + Base64.encode(data);
                talentsHomeVo.setAdvator(base64);
            }else{
                talentsHomeVo.setAdvator("");
            }

            //talentsHomeVo.setAdvator(itemTalent.getAdvator());
            talentsHomeVo.setAge(itemTalent.getTelnetAge());
            talentsHomeVo.setWorkJobAge(itemTalent.getWorkAge().toString());
            talentsHomeVo.setTalentName(itemTalent.getTelnetName());
            talentsHomeVo.setUniversityName(itemTalent.getGraduateUniversity());

            Integer educationId = itemTalent.getEducationId();
            EducationEntity educationEntity = educationDao.selectById(educationId);
            talentsHomeVo.setEducationBackground(educationEntity.getEducationName());

            QueryWrapper<ProvincesEntity> provincesWrapper = new QueryWrapper<>();
            provincesWrapper.eq("provinceid",itemTalent.getProvinceid());
            ProvincesEntity provincesEntity = provincesDao.selectOne(provincesWrapper);

            QueryWrapper<CitiesEntity> citiesWrapper = new QueryWrapper<>();
            citiesWrapper.eq("cityid",itemTalent.getCityid());
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesWrapper);

            QueryWrapper<AreasEntity> areasWrapper = new QueryWrapper<>();
            areasWrapper.eq("areaid",itemTalent.getAreaid());
            AreasEntity areasEntity = areasDao.selectOne(areasWrapper);

            String expAddress = provincesEntity.getProvincename() + "/" + citiesEntity.getCityname() + "/" + areasEntity.getAreaname();
            talentsHomeVo.setExpAddress(expAddress);

            QueryWrapper<TradeCategoryEntity> tradeCategoryWrapper = new QueryWrapper<>();
            tradeCategoryWrapper.eq("trade_id",itemTalent.getTradeId());
            TradeCategoryEntity tradeCategoryEntity = tradeCategoryDao.selectOne(tradeCategoryWrapper);

/*            QueryWrapper<TradeClassifyEntity> tradeClassifyWrapper = new QueryWrapper<>();
            tradeClassifyWrapper.eq("classify_id",itemTalent.getClassifyId());
            TradeClassifyEntity tradeClassifyEntity = tradeClassifyDao.selectOne(tradeClassifyWrapper);

            QueryWrapper<TradeJobEntity> tradeJobWrapper = new QueryWrapper<>();
            tradeJobWrapper.eq("job_type_id",itemTalent.getJobTypeId());
            TradeJobEntity tradeJobEntity = tradeJobDao.selectOne(tradeJobWrapper);

            String expIndustory = tradeCategoryEntity.getTradeName() + "/" + tradeClassifyEntity.getClassifyName() +
                    "/" + tradeJobEntity.getJobName();*/
            talentsHomeVo.setExpIndustory(tradeCategoryEntity.getTradeName());

            talentsHomeVoDataList.add(talentsHomeVo);
        }
        return R.ok().put("data",talentsHomeVoDataList);
    }
}
